package leetcode.f1t100;

/**
 * 合并两个有序数组,使用第一个数组返回
 * https://leetcode.com/problems/merge-sorted-array/description/
 *
 * @author lichx
 * @createTime 2024/2/4 16:13
 */
public class Q88_MergeSortedArray {

    public static void main(String[] args) {
        Q88_MergeSortedArray bean = new Q88_MergeSortedArray();
        int[] nums1 = {4,5,6,0,0,0};
        int[] nums2 = {1,2,3};
        bean.merge(nums1, 3, nums2, 3);
        for (int i : nums1) {
            System.out.println(i);
        }
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        m=m-1;
        n=n-1;
        while(m>=0 && n>=0){
            if(nums1[m]<=nums2[n]){
                nums1[m+n+1] = nums2[n--];
            }else{
                nums1[m+n+1] = nums1[m--];
            }
        }
        while(n>=0){
            nums1[n] = nums2[n--];
        }
    }
}
